<?php

if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Events extends AUTH_Controller {

  public function index() {
    $all_events = $this->all_events(array(1, 2, 3));
    $event_dates = NULL;
    if (!empty($all_events)) {
      foreach ($all_events as $up_event) {
        $event_dates[] = array(
            'date' => date('Y-m-d', strtotime($up_event['date'])),
            'type' => $up_event['type']
        );
      }
    }
    $event_dates = json_encode($event_dates);
    $this_events = $this->this_events(array(1, 2, 3));
    $view_data = array();
    $view_data['menu_active'] = 'events';
    $view_data['this_events'] = $this_events;
    $view_data['event_dates'] = $event_dates;
    $this->load_view('events', $view_data);
  }

  private function calender_events($date_data = NULL, $types = array()) {
    $event_data = array();
    $birthday_users = NULL;
    if (in_array(1, $types)) {
      $get_birthdays = $this->event_model->get_birthdays($date_data);
      $birthday_users = $get_birthdays['result'];
    }
    $joinday_users = NULL;
    if (in_array(2, $types)) {
      $get_joindays = $this->event_model->get_joindays($date_data);
      $joinday_users = $get_joindays['result'];
    }
    $user_events = NULL;
    if (in_array(3, $types)) {
      $get_user_event = $this->event_model->get_user_event($date_data);
      $user_events = $get_user_event['result'];
    }
    if (!empty($birthday_users) && is_array($birthday_users)) {
      foreach ($birthday_users as $bduser) {
        $event_data[] = array(
            'type' => 1,
            'date' => $bduser['user_birthday_date'],
            'user_id' => $bduser['user_id'],
            'user_name' => $bduser['user_name'],
            'user_display_name' => $bduser['user_display_name'],
            'user_image' => $bduser['user_image'],
            'event_msg' => ''
        );
      }
    }
    if (!empty($joinday_users) && is_array($joinday_users)) {
      foreach ($joinday_users as $jduser) {
        $event_data[] = array(
            'type' => 2,
            'date' => $jduser['user_join_date'],
            'user_id' => $jduser['user_id'],
            'user_name' => $jduser['user_name'],
            'user_display_name' => $jduser['user_display_name'],
            'user_image' => $jduser['user_image'],
            'event_msg' => ''
        );
      }
    }
    if (!empty($user_events) && is_array($user_events)) {
      foreach ($user_events as $user_event) {
        if (strtotime($user_event['event_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 3,
              'date' => $user_event['event_date'],
              'user_id' => $user_event['user_id'],
              'user_name' => $user_event['user_name'],
              'user_display_name' => $user_event['user_display_name'],
              'user_image' => $user_event['user_image'],
              'event_msg' => $user_event['event_message']
          );
        }
      }
    }
    return $event_data;
  }

  private function all_events($types = array(), $limit = NULL) {
    $event_data = array();
    $birthday_users = NULL;
    if (in_array(1, $types)) {
      $get_birthdays = $this->event_model->get_all_birthdays();
      $birthday_users = $get_birthdays['result'];
    }
    $joinday_users = NULL;
    if (in_array(2, $types)) {
      $get_joindays = $this->event_model->get_all_joindays();
      $joinday_users = $get_joindays['result'];
    }
    $user_events = NULL;
    if (in_array(3, $types)) {
      $get_all_user_events = $this->event_model->get_all_user_events();
      $user_events = $get_all_user_events['result'];
    }
    if (!empty($birthday_users) && is_array($birthday_users)) {
      foreach ($birthday_users as $bduser) {
        if (strtotime($bduser['user_birthday_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 1,
              'date' => $bduser['user_birthday_date'],
              'user_id' => $bduser['user_id'],
              'user_name' => $bduser['user_name'],
              'user_display_name' => $bduser['user_display_name'],
              'user_image' => $bduser['user_image'],
              'event_msg' => ''
          );
        }
      }
    }
    if (!empty($joinday_users) && is_array($joinday_users)) {
      foreach ($joinday_users as $jduser) {
        if (strtotime($jduser['user_join_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 2,
              'date' => $jduser['user_join_date'],
              'user_id' => $jduser['user_id'],
              'user_name' => $jduser['user_name'],
              'user_display_name' => $jduser['user_display_name'],
              'user_image' => $jduser['user_image'],
              'event_msg' => ''
          );
        }
      }
    }
    if (!empty($user_events) && is_array($user_events)) {
      foreach ($user_events as $user_event) {
        if (strtotime($user_event['event_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 3,
              'date' => $user_event['event_date'],
              'user_id' => $user_event['user_id'],
              'user_name' => $user_event['user_name'],
              'user_display_name' => $user_event['user_display_name'],
              'user_image' => $user_event['user_image'],
              'event_msg' => $user_event['event_message']
          );
        }
      }
    }
    @usort($event_data, array('events', 'date_compare'));
    if (!empty($limit) && is_numeric($limit)) {
      $event_data = @array_slice($event_data, 0, $limit);
    }
    return $event_data;
  }

  private function this_events($types = array(), $limit = NULL) {
    $event_data = array();
    $birthday_users = NULL;
    if (in_array(1, $types)) {
      $get_birthdays = $this->event_model->get_this_birthdays();
      $birthday_users = $get_birthdays['result'];
    }
    $joinday_users = NULL;
    if (in_array(2, $types)) {
      $get_joindays = $this->event_model->get_this_joindays();
      $joinday_users = $get_joindays['result'];
    }
    $user_events = NULL;
    if (in_array(3, $types)) {
      $get_all_user_events = $this->event_model->get_this_user_events();
      $user_events = $get_all_user_events['result'];
    }
    if (!empty($birthday_users) && is_array($birthday_users)) {
      foreach ($birthday_users as $bduser) {
        if (strtotime($bduser['user_birthday_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 1,
              'date' => $bduser['user_birthday_date'],
              'user_id' => $bduser['user_id'],
              'user_name' => $bduser['user_name'],
              'user_display_name' => $bduser['user_display_name'],
              'user_image' => $bduser['user_image'],
              'event_msg' => ''
          );
        }
      }
    }
    if (!empty($joinday_users) && is_array($joinday_users)) {
      foreach ($joinday_users as $jduser) {
        if (strtotime($jduser['user_join_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 2,
              'date' => $jduser['user_join_date'],
              'user_id' => $jduser['user_id'],
              'user_name' => $jduser['user_name'],
              'user_display_name' => $jduser['user_display_name'],
              'user_image' => $jduser['user_image'],
              'event_msg' => ''
          );
        }
      }
    }
    if (!empty($user_events) && is_array($user_events)) {
      foreach ($user_events as $user_event) {
        if (strtotime($user_event['event_date']) != '-62170005200') {
          $event_data[] = array(
              'type' => 3,
              'date' => $user_event['event_date'],
              'user_id' => $user_event['user_id'],
              'user_name' => $user_event['user_name'],
              'user_display_name' => $user_event['user_display_name'],
              'user_image' => $user_event['user_image'],
              'event_msg' => $user_event['event_message']
          );
        }
      }
    }
    @usort($event_data, array('events', 'date_compare'));
    if (!empty($limit) && is_numeric($limit)) {
      $event_data = @array_slice($event_data, 0, $limit);
    }
    return $event_data;
  }

  public function show_events() {
    if ($this->is_post() && $this->is_ajax()) {
      $date_data = $this->input->post('date_data', TRUE);
      if (!empty($date_data) && strtotime($date_data)) {
        $events_data = $this->calender_events($date_data, array(1, 2, 3));
        $view_data = array();
        $view_data['events_data'] = $events_data;
        $view_data['date_data'] = $date_data;
        $this->load->view('event_show_events', $view_data);
        return;
      }
      return;
    }
    redirect();
  }

  private function date_compare($a, $b) {
    $t1 = strtotime(date('md', strtotime($a['date'])));
    $t2 = strtotime(date('md', strtotime($b['date'])));
    return $t1 - $t2;
  }

}

?>